﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using System.Web;
using FacturIT.BLL.Model.Helper;
using FacturIT.BLL.Model.UserCompany;
using FacturIT.Service;

namespace FacturIT.Service.Repositories.userCompany
{
    public class UserCompanyRepository : IRepository<BLL.Model.UserCompany.UserCompany, int>
    {
        public UserCompanyRepository()
        {
            DB = new UserCompanyManagementDataContext(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DatabaseToUse"]].ConnectionString);
        }

        protected Table<BLL.Model.UserCompany.UserCompany> DT
        {
            get { return ((UserCompanyManagementDataContext)base.DB).UserCompanies; }
        }

        public override BLL.Model.UserCompany.UserCompany Read(int id)
        {
            BLL.Model.UserCompany.UserCompany UserCompany = ReadByID(id);

            return UserCompany == null ? null : UserCompany;
        }


        public override IEnumerable<BLL.Model.UserCompany.UserCompany> ReadAll()
        {
            return from UserCompany in DT
                   orderby UserCompany.CompanyID
                   select UserCompany;
        }


        internal IEnumerable<BLL.Model.UserCompany.UserCompany> ReadAllUserCompanyes(int user)
        {

            return from UserCompany in DT
                   where UserCompany.UserID.Equals(user)
                   orderby UserCompany.UserCompanyID
                   select UserCompany;
        }

        public override IEnumerable<BLL.Model.UserCompany.UserCompany> ReadAll(params int[] ids)
        {
            return from UserCompany in DT
                   where ids.Contains(UserCompany.UserCompanyID)
                   select UserCompany;
        }

        public override BLL.Model.UserCompany.UserCompany Add(BLL.Model.UserCompany.UserCompany t)
        {

            DT.InsertOnSubmit(t);
            SaveChanges();
            return Read(t.UserCompanyID);
        }

        public override bool Delete(BLL.Model.UserCompany.UserCompany t)
        {

            DT.DeleteOnSubmit(t);
            SaveChanges();
            return !Exists(t.UserCompanyID);
        }

        public override BLL.Model.UserCompany.UserCompany Update(BLL.Model.UserCompany.UserCompany t)
        {
            UpdateObject(t, ReadByID(t.UserCompanyID), false);
            SaveChanges();
            return ReadByID(t.UserCompanyID);
        }

        public override BLL.Model.UserCompany.UserCompany Duplicate(BLL.Model.UserCompany.UserCompany t)
        {
            return Add(Create(t.CompanyID, t.UserID));
        }

        protected override BLL.Model.UserCompany.UserCompany ReadByID(int id)
        {
            return DT.SingleOrDefault(x => x.UserCompanyID == id);
        }

        protected override void UpdateObject(BLL.Model.UserCompany.UserCompany left, BLL.Model.UserCompany.UserCompany right, bool replaceID)
        {
            if (replaceID) left.UserCompanyID = right.UserCompanyID;
            left.CompanyID = right.CompanyID;
            left.UserID = right.UserID;
            
        }

        protected override IEnumerable<BLL.Model.UserCompany.UserCompany> ApplyOrdering(IEnumerable<BLL.Model.UserCompany.UserCompany> query, IDictionary<Func<BLL.Model.UserCompany.UserCompany, object>, FacturIT.Service.Enum.Ordering> columns)
        {
            return OrderHelper.ApplyOrdering<BLL.Model.UserCompany.UserCompany, object>(query, columns, phone => phone.CompanyID);
        }
        public BLL.Model.UserCompany.UserCompany Create(int companyid, int userid)
        {
            return new BLL.Model.UserCompany.UserCompany() { CompanyID = companyid, UserID = userid };
        }


        internal IEnumerable<BLL.Model.UserCompany.UserCompany> ReadMyCompanies(int userid)
        {
            return from usercompany in DT
                   where usercompany.UserID == userid
                   select usercompany;
        }
    }

}